package com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.category

import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.fastjson.JSON
import com.chad.library.adapter.base.BaseQuickAdapter
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.networking.RestClient
import com.example.library_common.utils.PixUtils
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.category.bean.GoodsListBean
import com.example.shushiyunsst.util.*
import com.example.wallet.global.AppGlobals
import com.example.wallet.utils.selectorIcon
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

@Route(path = GOODS_LIST)
class GoodsListFragment :SimpleListFragment<GoodsListBean>() {

    @JvmField
    @Autowired(name = "categoryId")
    var categoryId: String = ""

    private var  cml= selectorIcon()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isAddDividerItemDecoration=false

    }

    override fun initView(view: View) {
        super.initView(view)
        view.setBackgroundResource(R.color.bg_color)
        mHeader.removeAllViews()
        addHeader(R.layout.comment_header_layout)
        mHeader.setBackgroundResource(R.color.bg_color_0)
        val mToolbar: Toolbar=view.findViewById(R.id.mToolbar)
        mToolbar.setTitle(R.string.goods_list)
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }
        mRecyclerView.setPadding(0,0,PixUtils.dp2px(10),0)
    }

    override fun getItemLayout(): Int {
        return R.layout.goods_list_fragment
    }

    override fun convertItem(helper: JssBaseViewHolder?, item: GoodsListBean?) {
        val giveCmlStr=_mActivity.getString(R.string.wallet_fu_li_scored)
        helper?.setText(R.id.goodsName,item?.goodsName)
            ?.setText(R.id.goodsPrice,item?.goodsPrice)
            ?.setImageNetUrl(R.id.headerPic,item?.defaultPic,R.mipmap.image_loading)
            ?.setText(R.id.payedCount,giveCmlStr)
//            ?.setText(R.id.payedCount,"赠送${item?.givenCml} $cml")
    }

    override fun getLayoutManager(): RecyclerView.LayoutManager {
        return GridLayoutManager(_mActivity,2)
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(GoodsListBean::class.java).endSubType().build()
    }

    override fun netRequest() {
        val maps = mapOf(
            "catId" to categoryId,
            "pageCount" to Int.MAX_VALUE,
            "pageIndex" to pageIndex
        )

        RestClient.builder()
            .url(R.string.queryGoodsByCatId)
            .raw(JSON.toJSONString(maps))
            .success {

                parseDate(it)

            }.failure {
                onFailed()
            }.error { _, _ ->
                onFailed()
            }
            .build()
            .post()
    }



    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val parse = JSON.parseObject(data)
        val goodsList = parse["goodsList"]
        val array = JSON.parseArray(goodsList.toString(), GoodsListBean::class.java)
        loadListDate(array)
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        super.onItemClick(adapter, view, position)
        val item = mAdapter.getItem(position)
//        ARouter.getInstance()
//            .build(GOODS_DETAIL)
//            .withString(GOODS_ID, item.gid.toString())
//            .startNavigation(this)

        ARouter.getInstance()
            .build(GOODS_DETAIL)
            .withString(GOODS_ID, item.gid.toString())
            .startNavigation(this)
    }
}